package me.dynamicarray;

import java.util.Arrays;
import java.util.Iterator;

public class DynamicArray implements Iterable<Integer> {
    private int size;
    private int capacity;
    private int[] array;

    //构造方法：初始化容量（默认5）
    public DynamicArray() {
        this(5);
    }

    //构造方法：指定初始容量
    public DynamicArray(int initialCapacity) {
        if (initialCapacity <= 0) {
            //容量不合法时，使用默认值初始化
            this.capacity = 5;
            this.array = new int[this.capacity];
            this.size = 0;
        } else {
            this.capacity = initialCapacity;
            this.array = new int[capacity];
            this.size = 0;
        }

    }

    //添加元素
    public boolean add(int index, int element) {
        //检查索引合法性
        if (index < 0 || index > size) {
            return false;
        }
        checkAndGrow();
        //添加逻辑：向后挪动，空出待插入位置
        if (index < size) {
            System.arraycopy(array, index, array, index + 1, size - index);
        }
        array[index] = element;
        size++;
        return true;
    }

    //在最后添加
    public boolean addLast(int element) {
        return add(size, element);
    }

    //扩容
    private void checkAndGrow() {
        //容量检查
        if (size == 0) {
            array = new int[capacity];
        } else if (size == capacity) {
            //扩容，1.5倍
            capacity = capacity + (capacity >> 1);
            int[] newArray = new int[capacity];
            //把数据拷贝到新数组中
            System.arraycopy(array, 0, newArray, 0, size);
            array = newArray;
        }
    }

    //删除
    public int remove(int index) {
        //检查索引合法性
        if (index < 0 || index >= size) {
            return -1;
        }
        //先记录被删除的元素
        int removed = array[index];
        if (index < size - 1) {
            //向前挪动
            System.arraycopy(array, index + 1, array, index, size - index - 1);
        }
        size--;
        return removed;
    }

    //查询元素
    public int get(int index) {
        if (index < 0 || index >= size) {
            return -1;
        }
        return array[index];
    }

    //获取元素数量
    public int getSize() {
        return size;
    }

    //遍历一
    //创建数组副本，仅拷贝有效元素
    public int[] toArray() {
        return Arrays.copyOf(array, size);

    }
    //遍历二
    //重写toString


    @Override
    public String toString() {
        if (size == 0) {
            return "[]";
        }
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < size; i++) {
            sb.append(array[i]);
            if (i != size - 1) {
                sb.append(",");
            }
        }
        sb.append("]");
        return sb.toString();
    }

    //迭代器遍历
    //返回迭代器对象
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            private int currentIndex = 0;

            @Override
            public boolean hasNext() {
                return currentIndex < size;
            }

            @Override
            public Integer next() {
                //先返回array[currentIndex]，再++
                return array[currentIndex++];
            }
        };
    }
}
